package org.morningangel.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.naming.NamingException;

import org.morningangel.bean.Opinion;
import org.morningangel.dao.OpinionDao;
import org.morningangel.util.DBHelperUtil;

public class OpinionDaoImpl implements OpinionDao {

	@Override
	public Integer save(Opinion opinion) {
		Connection conn = null;
		PreparedStatement ps = null;
		Integer suNum = 0;
		try {
			conn = DBHelperUtil.getConnection();
			//打开事物
			conn.setAutoCommit(false);
			String sql = "insert into tb_opinion (userId, content, time) values(?, ?, ?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, opinion.getUserId());
			ps.setString(2, opinion.getContent());
			ps.setTimestamp(3, new Timestamp(opinion.getTime().getTime()));
			suNum = ps.executeUpdate();
			//提交事务
			conn.commit();
			//关闭事务
			conn.setAutoCommit(true);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHelperUtil.close(ps, conn);
		
		return suNum;
	}

	@Override
	public List<Opinion> listPage(Integer pageNum, Integer pageSize) {
		return null;
	}

	@Override
	public Opinion getById(Integer id) {
		return null;
	}

}
